let Vue;
class Store{
  constructor(options){
    const computed={}
    this.getters={}

    const store=this
    this._wrapperGetters=options.getters
    Object.keys(this._wrapperGetters).forEach(key=>{
      computed[key]=function (){
        return store._wrapperGetters[key](store.state)
      }
      Object.defineProperty(store.getters,key,{
        get (){
          return store.state[key]
        }
      })
    })

    this.state=new Vue({
      data(){
        return options.state
      },
      computed
    })
    this.$mutations=options.mutations
    this.$actions=options.actions
  }
  commit(type,payload){
    const entry=this.$mutations[type]
    entry(this.state,payload)
  }
  dispatch(type,payload){
    const entry=this.$actions[type]
    entry(this,payload)
  }
}

function install(_vue){
  Vue=_vue
  Vue.mixin({
    beforeCreate() {
      console.log(this,'this')
      if(this.$options.store){
        Vue.prototype.$store=this.$options.store
      }
    }
  })
}
export default {Store,install}
